package com.c.ctgproject.ui.collect

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.ImageCommonRvV2Adapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryGetWeightBinding
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/16
 *描述: 三件过磅 页面(动力电池)
 */
class BatteryGetWeightActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityBatteryGetWeightBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val imageResultCode = 100
    private val imageShowList = ArrayList<String>()
    private val orderId by lazy { intent.getIntExtra(MyConstants.ORDER_ID, -1).handlerNull() }
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }
    private val mOrderIdList by lazy {
        intent.getStringExtra(MyConstants.ORDER_ID_LIST)
    }
    override fun event() {
        mBinding.mPhotoNGV.init(this, imageShowList, 1, imageResultCode)
       if(orderId!="-1"){
           ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryDetail(orderId), MyObserverHandlerLoading(this, loading) {
               MyLog.d("电池详情回调:$it")
               val jsonObject = JSONObject(it)
               val data = jsonObject.getJSONObject("data")
               mBinding.poundNo.setText(data.optString("poundNo", "").handlerNull())
               mBinding.poundWeight.setText( data.optString("poundWeight", "").handlerNull())
               mBinding.purchasePoundNo.setText(data.optString("purchasePoundNo", "").handlerNull())
               mBinding.purchaseWeight.setText(data.optString("purchaseWeight", "").handlerNull())
               mBinding.mRemarkET.setText(data.optString("poundRemark", "").handlerNull())
               val poundPicture=data.optString("poundPicture", "").handlerNull()
               if(poundPicture.isNotEmpty()){
                   val resultList: MutableList<NineGridBean> = ArrayList()
                   val nineGirdData = NineGridBean(poundPicture)
                   imageShowList.add(poundPicture)
                   resultList.add(nineGirdData)
                   mBinding.mPhotoNGV.addDataList(resultList)
               }

           })
       }
    }

    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
                MyLog.d("${getGson().toJson(imageShowList)}")
                if(imageShowList.size>0){
                    onHttp(imageShowList[0])
                }else{
                    showToast("请上传过磅图片")
                }
        }
    }


    private fun onHttp(imageUrl: String? = null) {
        val poundNo = mBinding.poundNo.text.handlerNull()
        val poundWeight = mBinding.poundWeight.text.handlerNull()
        val purchasePoundNo = mBinding.purchasePoundNo.text.handlerNull()
        val purchaseWeight = mBinding.purchaseWeight.text.handlerNull()
        val poundRemark = mBinding.mRemarkET.text.handlerNull()
        when {
            poundNo.isEmpty() -> showToast("请输入磅单号")
            poundWeight.isEmpty() -> showToast("请输入磅单重量")
            else -> {
                val map = HashMap<String, Any>()
                if(orderId!="-1"){
                    map["orderId"] = orderId
                }
                map["poundNo"] = poundNo
                map["orderIdList"]= Gson().fromJson(mOrderIdList,ArrayList<Int>()::class.java)
                map["poundWeight"] = poundWeight
                if (purchasePoundNo.isNotEmpty()) map["purchasePoundNo"] = purchasePoundNo
                if (purchaseWeight.isNotEmpty()) map["purchaseWeight"] = purchaseWeight
                if (poundRemark.isNotEmpty()) map["poundRemark"] = poundRemark
                if (imageUrl != null && imageUrl.isNotEmpty()) {
                    map["poundPicture"] = imageUrl
                }
                ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryPound(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("提交回调:$it")
                    showToast("提交成功")
                    EventBus.getDefault().post(PositionEvent(mListPosition))
                    setResult(1)
                    finish()
                })
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    list?.let {
                        if(list.size>0){
                            val file = File(list[0].imagePath)
                            //图片压缩
//                val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                val bitmap = BitmapFactory.decodeFile(newFile.absolutePath)
//                val bitmaResult = ImageShuiYinUtil.createWaterMaskRightBottom(this, bitmap, "广汽内场管理系统", 10, 10)
//                val  ff=saveBitmapFile(this,bitmaResult,1)
//                val ff=ImageUtil.imageWaterMark(this,file,1)
                            val ff: File = WaterRemark.handlerWaterRemark2File(this, file, "1")


                            upDataFile(arrayListOf(ff)) {
                                if (it != null && it.isNotEmpty()) {
                                    //onHttp(it[0].fileUrl.handlerNull())
                                    val resultList: MutableList<NineGridBean> = ArrayList()
                                    it[0].fileUrl?.let {
                                        val nineGirdData = NineGridBean(it)
                                        imageShowList.add(it)
                                        resultList.add(nineGirdData)
                                        mBinding.mPhotoNGV.addDataList(resultList)
                                    }

                                } else {
                                    showToast("图片上传异常请稍后再试")
                                }


                            }

                        }
                    }


                }
                else -> {

                }
            }
        }
    }
}